package cn.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.servlet.MultipartConfigElement;


/**
 *
 */
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
    @Value("${web.upload-path}")
    private String path;
    @Value("${external.access.path:}")
    private String getPath;
    /**
     * 资源文件外化
     *
     * @param registry 资源处理注册
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(getPath).addResourceLocations("file:"+path);
        super.addResourceHandlers(registry);
    }

    /**
     * 设置上传照片大小
     * @return
     */
    @Bean
    public MultipartConfigElement multipartConfigElement(){
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //文件最大KB,MB
        factory.setMaxFileSize("2MB");
        //设置总上传数据总大小
        factory.setMaxRequestSize("10MB");
        return factory.createMultipartConfig();
    }
}
